<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\model\TypeModel;
//楼盘参数管理

class Type extends Common
{   
  
    //添加
    public function add()
    {   
        if(Request::instance()->isPost()){
          $post=input('post.');
          $da=TypeModel::where('typename',$post['typename'])->where('pid',$post['pid'])->find();
          if($da){
            return ['code'=>0,'msg'=>'该类别已存在','data'=>''];
          }
          if(isset($req['sort'])){
            if(!$req['sort'] || !is_numeric($req['sort'])||strpos($req['sort'],".")!==false ||$req['sort']<0){
                return ['code'=>0,'msg'=>'序号值为正整数!','data'=>''];
            }
          }
          if($post['pid']==0){
            $post['path']='0';
            $post['level']=1;
          }else{  
            $parent=TypeModel::get($post['pid']);
            $post['path']=$parent['path'].','.$post['pid'];
            $post['level']=$parent['level']+1;
          }
          
          
          unset($post['image']);
          $post['sort']=TypeModel::where('pid',$post['pid'])->count();
          $post['sort']+=1;
          $post['addtime']=time();
          $type = TypeModel::create($post);
          if($type){
            return ['code'=>1,'msg'=>'添加成功','data'=>''];
          }else{
            return ['code'=>0,'msg'=>'添加失败','data'=>''];
          }
        }else{
          $get=input('get.');
          $pid=$get['pid'];
          $this->assign('pid',$pid);
        }

        return $this->fetch();

    }
    public function up_sort(){
      $id=input('post.id');
      $sort=input('post.sort');
      if(!$sort || !is_numeric($sort)||strpos($sort,".")!==false || $sort<0){
              return 3;
      }
      $re=Db::table('type')->where('id',$id)->update(['sort'=>$sort]);
      return $re;
    }
  //修改
  public function update()
  {   
      $id=input('get.id');
      $data=TypeModel::get($id);
      if(Request::instance()->isPost()){
        $post=input('post.');
        if(isset($post['sort'])){
            if(!$post['sort'] || !is_numeric($post['sort'])||strpos($post['sort'],".")!==false ||$post['sort']<0){
                return ['code'=>0,'msg'=>'序号值为正整数!','data'=>''];
            }
          }
        unset($post['image']);
        $da=TypeModel::where('typename',$post['typename'])->where('pid',$data['pid'])->where('id','<>',$id)->find();
        if($da){
          return ['code'=>0,'msg'=>'该类别已存在','data'=>''];
        }
        $num=TypeModel::where('id', $id)->update($post);
        if($num){
          return ['code'=>1,'msg'=>'修改成功','data'=>''];
        }else{
          return ['code'=>0,'msg'=>'修改失败','data'=>''];
        }
      }else{
        $this->assign('data',$data);
        return $this->fetch();
      }

      

  }

    public function up_status()
    { 

       $id=input("get.id");

       $data=Db::table('type')->where('id',$id)->find();

       $status=$data['status'];

       if($status==1){  //当前启用

          $num=Db::table('type')->where('id',$id)->update(['status'=>2]);

          if($num>0){

               return 1;

          }else{

               return 2;

          }

      }else{

          $num=Db::table('type')->where('id',$id)->update(['status'=>1]);

          if($num>0){

               return 3;

          }else{

               return 4;

          }

       }

    }
  //人员列表
  public function type()
  {   
      $type=TypeModel::all();
      $this->assign('type',$type);
      return $this->fetch();
  }
  public function lst()
  { 
        $req=input("post.");
        $where=[];
        if(isset($req['pid'])){
          $where['pid']=$req['pid'];
        }
        if(isset($req['id']) && !empty($req['id'])){
          $where['id']=$req['id'];
        }
  
        $data=[];
        $list=TypeModel::where($where)->order('sort asc')->select();
        for ($i=0; $i <count($list) ; $i++) { 
          $zi=TypeModel::where('pid',$list[$i]['id'])->order('sort asc')->select();
          if(count($zi)>0){
            $list[$i]['haveChild']=true;
          }else{
            $list[$i]['haveChild']=false;
          }
          $list[$i]['open']=false;  //折叠
          
        }

        return ['code'=>200,'data'=>$list];
  }
  
  //执行删除
  public function do_del()
  { 
      $data=input("post.");
      $id=input("post.id");
      $type=TypeModel::get($id);
      $count=TypeModel::where('pid',$type['id'])->count();
      if($count>0){  //还有子类
        return 2;
      }
      $num=$type->delete();
      return $num;
    
  }
  //文件上传
    public function upload(){
      $file = request()->file('image');
      $path='type';
      return $this->uploads($file,$path);

    }
}




